//
//  PlayViewController.m
//  LouMusicDemo
//
//  Created by 漫步人生路 on 15/7/1.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "PlayViewController.h"
#import "BlurImageView.h"
#import "LORequestManger.h"
#import "LanOuMusic.h"
#import "CUSFlashLabel.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height


@interface PlayViewController ()<LanOuMusicDelegate>
@property (nonatomic, strong) BlurImageView *blurImageView;


@property (nonatomic, strong) UIImageView *rotatView;
@property (nonatomic, strong) UISlider *progressView;

//闪光Label,初始化完给定一个textColor，并且执行stat方法开始闪动
//@property (nonatomic, strong) CUSFlashLabel *faceBookLabel;

@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //遵循代理
    [LanOuMusic shareMusic].delegate = self;
    
    self.rotatView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 400, 80, 80)];
    self.rotatView.image = [UIImage imageNamed:@"何冰冰.jpg"];
    self.rotatView.layer.masksToBounds = YES;
    self.rotatView.layer.cornerRadius = self.rotatView.bounds.size.height / 2;
    [self.view addSubview:self.rotatView];
    self.progressView = [[UISlider alloc]initWithFrame:CGRectMake(50, 500, 300, 10)];
    self.progressView.minimumValue = 0;
    [self.progressView addTarget:self action:@selector(seekToTime:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.progressView];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];
    play.frame = CGRectMake(100, 100, 100, 40);
    [play setTitle:@"播放" forState:UIControlStateNormal];
    [play addTarget:self action:@selector(playSong) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:play];
    
    
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeSystem];
    stop.frame = CGRectMake(100, 200, 100, 40);
    [stop setTitle:@"暂停" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(stopSong) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stop];
 
    UIButton *dismiss = [UIButton buttonWithType:UIButtonTypeSystem];
    dismiss.frame = CGRectMake(100, 300, 100, 40);
    [dismiss setTitle:@"返回" forState:UIControlStateNormal];
    [dismiss addTarget:self action:@selector(GoingToBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dismiss];
    //开始解析
    [self starMusic];

}
//开始解析
- (void)starMusic
{
    //判断我们单例里的index和songID是否和我点进来后的index、songID一样，如果一样的话继续播放我们之前的歌曲
    if ( [LanOuMusic shareMusic].index == self.index && [[LanOuMusic shareMusic].songID isEqualToString:self.songID])
    {
        if (![[LanOuMusic shareMusic] isPlayingCurrentAudioWithUrl:[LanOuMusic shareMusic].songUrl]) {
            [[LanOuMusic shareMusic] playMusicWithUrl:[LanOuMusic shareMusic].songUrl];
            [[LanOuMusic shareMusic] starPlayMusic];
        }
        //否则就重新加载
    }else
    {
        [[LanOuMusic shareMusic] loadDataWithSongsWithSongID:self.songID];
        [LanOuMusic shareMusic].index = self.index;
        [LanOuMusic shareMusic].songID = self.songID;

    }
}
//返回
- (void)GoingToBack
{

    [self dismissViewControllerAnimated:YES completion:nil];
}
//开始播放
- (void)playSong
{
    [[LanOuMusic shareMusic] starPlayMusic];
    NSLog(@"开始播放");
}
//暂停播放
- (void)stopSong
{
    [[LanOuMusic shareMusic]stopMusic];
    NSLog(@"暂停播放");
}
//播放中执行
- (void)audioStreamer:(LanOuMusic *)streamer didPlayingWithProgress:(float)progress
{
    //闪动
    self.progressView.minimumTrackTintColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1];
    self.progressView.maximumTrackTintColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1];
    //将slider的最大值设置好
    self.progressView.maximumValue = [LanOuMusic shareMusic].maxValue.floatValue;
    
    //将每次执行此方法时的progress值赋给slider
    self.progressView.value = progress;
    
    //如果正在播放，就让我们的图片旋转
    if ([LanOuMusic shareMusic].isPlaying == YES) {
        self.rotatView.transform = CGAffineTransformRotate(self.rotatView.transform, 0.01);
    }
    //否则就空实现，什么都不做，相当于取消了旋转
    else
    {
        
    }

}
//快进
- (void)seekToTime:(UISlider *)sender
{
    [[LanOuMusic shareMusic] seekToTime:[sender value]];
}
//- (void)audioStreamerDidFinishPlaying:(LanOuMusic *)streamer
//{
//    [[LanOuMusic shareMusic] didFinsh];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
